#include "CImagePool.h"

CImagePool* CImagePool::iImagePool = NULL;

CImagePool* CImagePool::NewL()
{
	CImagePool* self = CImagePool::NewLC();
	CleanupStack::Pop(self);
	
	return self;
}

CImagePool* CImagePool::NewLC()
{
	CImagePool* self = new(ELeave) CImagePool;
	CleanupStack::PushL(self);
	self->ConstructL();
	
	return self;
}

void CImagePool::ConstructL()
	{
	
	}

CImagePool::CImagePool()
{
}

CImagePool::~CImagePool()
{
	iImageArray.Reset();
	iImageArray.Close();
}

CImage* CImagePool::Find(const THcImageLocation& aLocation)
{
	for(TInt i = 0; i < iImageArray.Count(); i++)
		{
		if(iImageArray[i]->GetImageLocation() == aLocation)
			{
			return iImageArray[i];
			}
		}
	
	return NULL;
}

CImage* CImagePool::Add(const THcImageLocation& aLocation)
{
	CImage* image = Find(aLocation);
	
	if(image)
		{
		image->AddRef();
		}
	else
		{
		image = CImage::CreateImage(aLocation);
		if(image)
			image->AddRef();
		}
	
	return image;
}